home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Effect library / Demo ƒ / Wipes ƒ / Dissolve wipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-08  |  4.5 KB  |  149 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        Dissolve wipe.c
  4.  
  5. Purpose:    Graphic effect from offscreen bitmap to main window (on
  6.             screen).  See comments below for more description.
  7.  
  8.  
  9. MSG Demo -- graphic effects demonstration program
  10. Copyright (C) 1992-4 Mark Pilgrim & Dave Blumenthal
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "msg timing.h"
  30.  
  31. #define CorrectTime 2
  32. #define theWindowWidth (boundsRect.right-boundsRect.left)
  33. #define theWindowHeight (boundsRect.bottom-boundsRect.top)
  34.  
  35. pascal short DissolveWipe(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect);
  36. pascal short DissolveBox(GrafPtr, GrafPtr, Rect*, Rect*);
  37.  
  38. /* the actual dissolve wipe splits the screen into 9 blocks, and dissolves
  39. the source onto the destination by blocks in random order.  That way, each
  40. dissolve is faster, and the whole effect is more interesting. */
  41.  
  42. pascal short DissolveWipe(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect)
  43. {
  44.     char            order[9];
  45.     char            ordertemp;
  46.     int                i;
  47.     long            randtemp;
  48.     Rect            source, dest;
  49.     int                resultCode;
  50.     
  51.     for(i = 0; i < 9; i++)
  52.         order[i] = i;
  53.     
  54.     for(i = 8; i >= 0; i--) {
  55.         randtemp = (((long)Random() + 32767) * (i + 1)) / 65535;
  56.         ordertemp = order[randtemp];
  57.         order[randtemp] = order[i];
  58.         order[i] = ordertemp;
  59.     }
  60.     
  61.     for(i = 0; i < 9; i++) {
  62.         source.top = (order[i] / 3) * (theWindowHeight / 3);
  63.         source.left = (order[i] % 3) * (theWindowWidth / 3);
  64.         source.bottom = (((order[i] / 3) + 1) * theWindowHeight) / 3;
  65.         source.right = (((order[i] % 3) + 1) * theWindowWidth) / 3;
  66.         OffsetRect(&source, boundsRect.left, boundsRect.top);
  67.         
  68.         resultCode=DissolveBox(sourceGrafPtr, destGrafPtr, &source, &source);
  69.         if (resultCode!=0)
  70.             return resultCode;    /* error, stop & return error code */
  71.     }
  72.     
  73.     return 0;
  74. }
  75.  
  76. pascal short DissolveBox(GrafPtr newImage, GrafPtr destGrafPtr, Rect *source, Rect *dest)
  77. {
  78.     long        offRowBytes, sizeOfOff;
  79.     Ptr            myBits;
  80.     Rect        bRect;
  81.     GrafPort    myGrafPort;
  82.     GrafPtr        myGrafPtr;
  83.     RgnHandle    oldClipRgn;
  84.     int            i, j;
  85.     Pattern        thePattern;
  86.     char        order[16];
  87.     long        randtemp;
  88.     char        ordertemp;
  89.     
  90.     /* the dissolve effect works by creating a random set of patterns which sum
  91.     (OR) to a black pattern.  We make another offscreen bitmap and fill it with
  92.     the pattern at each stage, and use it as a mask for the bitcopy.  Here, we
  93.     create the offscreen bitmap. */
  94.     
  95.     SetPort(newImage);
  96.     bRect = *source;
  97.     myGrafPtr = &myGrafPort;
  98.     OpenPort(myGrafPtr);
  99.     offRowBytes = (((source->right - source->left) + 15) >> 4) << 1;
  100.     sizeOfOff = (long)(source->bottom - source->top) * offRowBytes;
  101.     OffsetRect(&bRect, -bRect.left, -bRect.top);
  102.     myBits = NewPtr(sizeOfOff);
  103.     if(myBits == 0L)
  104.         return -1;        /* memory error */
  105.     myGrafPort.portBits.baseAddr = myBits;
  106.     myGrafPort.portBits.rowBytes = offRowBytes;
  107.     myGrafPort.portBits.bounds = bRect;
  108.     myGrafPort.portRect = bRect;
  109.     oldClipRgn = myGrafPort.clipRgn;
  110.     myGrafPort.clipRgn = destGrafPtr->visRgn;
  111.     
  112.     for(i = 0; i < 16; i++)
  113.         order[i] = i;
  114.     
  115.     /* this randomly shuffles the order in which the pattern bits will appear */
  116.     for(i = 15; i >= 0; i--) {
  117.         randtemp = (((long)Random() + 32767) * (i + 1)) / 65535;
  118.         ordertemp = order[randtemp];
  119.         order[randtemp] = order[i];
  120.         order[i] = ordertemp;
  121.     }
  122.     
  123.     for(i = 0; i < 16; i++)
  124.     {
  125.         StartTiming();
  126.         SetPort(myGrafPtr);
  127.         
  128.         for(j = 0; j < 8; j++) thePattern[j] = 0;
  129.         thePattern[order[i] >> 2] = 1 << (order[i] & 0x03);
  130.         thePattern[order[i] >> 2] |= 16 << (order[i] & 0x03);
  131.         thePattern[(order[i] >> 2) + 4] = 1 << (order[i] & 0x03);
  132.         thePattern[(order[i] >> 2) + 4] |= 16 << (order[i] & 0x03);
  133.         
  134.         FillRect(&bRect, &thePattern);
  135.         
  136.         SetPort(destGrafPtr);
  137.         
  138.         CopyMask(&(newImage->portBits), &(myGrafPtr->portBits),
  139.                 &(destGrafPtr->portBits), source, &bRect, dest);
  140.         TimeCorrection(CorrectTime);
  141.     }
  142.     
  143.     myGrafPort.clipRgn = oldClipRgn;
  144.     ClosePort(myGrafPtr);
  145.     DisposPtr(myBits);
  146.     
  147.     return 0;
  148. }
  149.